with sharing global class MsrRequestInfoUpdate {
    global class BudgetSurplus{
        webservice Id ProductId;
        webservice Id HospitalId;
        webservice Double Value;
        webservice String debug;
        webservice Boolean InventoryHaveProduct;//true,仓库有货。false,仓库没货。
    }
        
    global class WebToPDAMedical{
        webservice List<Product2> Products; 
        webservice List<BudgetSurplus> Surplus;
    }
    
    global class inputClass{
        webservice Boolean downloadAll;
        webservice date lastRequestDate;
    }
    
    public static List<Product2> ReturnProduct()
    {
        List<Product2> ProductList = [Select p.SKU_Code__c, p.Id,p.Product_Unit_Volume__c,p.Name From Product2 p where p.Product_Type__c =: 'PE Sample'];
        return ProductList;  
    }   
    
    public static List<BudgetSurplus> ReturnBudgetSurplus(Boolean downloadAll, date lastRequestDate)
    {
       List<BudgetSurplus> returnSurplus = new List<BudgetSurplus>();
       List<AggregateResult> HosMonAllocatItem = new List<AggregateResult>();
       List<AggregateResult> PeRequestItem = new List<AggregateResult>();
       
       if(downloadAll)
       { 

       // for test
           HosMonAllocatItem = [Select Product__c pro,  HospitalIdText__c hos, sum(unit__c) kg From HospitalMonthlyAllocationItem__c 
                Where  Owner__c =: UserInfo.getUserId().substring(0,15) and Year__c =: string.valueof(date.today().year() + 1) and Month__c =: string.valueof(date.today().month()) group by Product__c,HospitalIdText__c];           
           PeRequestItem = [Select Product__c pro,  HospitalIdText__c hos, sum(SendUnit__c) kg From PESampleRequestItem__c Where Year__c =: date.today().year() and Month__c =: date.today().month() group by Product__c, HospitalIdText__c];
                       
       }
       else
       {
           List<HospitalMonthlyAllocationItem__c> ChangeHosMonAllocatItem = [Select id From HospitalMonthlyAllocationItem__c Where LastModifyDate__c >=: lastRequestDate];
           List<PESampleRequestItem__c> ChangePeRequestItem = [Select id From PESampleRequestItem__c Where LastModifyDate__c >=: lastRequestDate];
           if((ChangeHosMonAllocatItem.size() > 0)||(ChangePeRequestItem.size() > 0))
           { 

           // for test    
               HosMonAllocatItem = [Select Product__c pro,  HospitalIdText__c hos, sum(unit__c) kg From HospitalMonthlyAllocationItem__c 
                    Where  Owner__c =: UserInfo.getUserId().substring(0,15) and Year__c =: string.valueof(date.today().year() + 1) and Month__c =: string.valueof(date.today().month()) group by Product__c,   HospitalIdText__c limit 1];               
               PeRequestItem = [Select Product__c pro,  HospitalIdText__c hos, sum(SendUnit__c) kg From PESampleRequestItem__c Where Year__c =: date.today().year() and Month__c =: date.today().month() group by Product__c,  HospitalIdText__c limit 1];       
           }
       }    
       
       for(AggregateResult ag : HosMonAllocatItem)
       {
           if(PeRequestItem.size() > 0){
               Boolean havePequest = false;
               for(AggregateResult pe : PeRequestItem)
               {
                   if(string.valueof(ag.get('pro')) == string.valueof(pe.get('pro'))&&(string.valueof(ag.get('hos')) == string.valueof(pe.get('hos'))))
                   {
                       BudgetSurplus  BS= new BudgetSurplus();
                       if((Double)ag.get('kg') > (Double)pe.get('kg'))
                           BS.Value = (Double)ag.get('kg') - (Double)pe.get('kg');
                       else
                           BS.Value = 0;
    
                        BS.ProductId = string.valueof(ag.get('pro'));
                        BS.HospitalId = string.valueof(ag.get('hos'));
                        BS.InventoryHaveProduct = true;
                        BS.debug = 'Start';
                        returnSurplus.add(BS);
                        havePequest = true;
                   }      
               }
               if(!havePequest)
               {
                    BudgetSurplus  BS= new BudgetSurplus();
                    BS.Value = (Double)ag.get('kg');
                    BS.ProductId = string.valueof(ag.get('pro'));
                    BS.HospitalId = string.valueof(ag.get('hos'));
                    BS.InventoryHaveProduct = true;
                    returnSurplus.add(BS);               
               }
           }
           else
           {
               BudgetSurplus  BS= new BudgetSurplus();

                BS.Value = (Double)ag.get('kg');
                BS.ProductId = string.valueof(ag.get('pro'));
                BS.HospitalId = string.valueof(ag.get('hos'));
                BS.InventoryHaveProduct = true;
                BS.debug = 'END';
                returnSurplus.add(BS);
           }
       }

        return returnSurplus;

    }      
       
    webService static WebToPDAMedical getIncrementalMedical(inputClass FromPDA)
    {
        WebToPDAMedical ReturnBack = new WebToPDAMedical();
        
        if(FromPDA.downloadAll = true)
        {
            ReturnBack.Products = ReturnProduct();
            ReturnBack.Surplus = ReturnBudgetSurplus(FromPDA.downloadAll,FromPDA.lastRequestDate);
        }
        
        
        return ReturnBack;
    }
     static testMethod void testMedicalIncrementalChangesWS () {
        User u = [Select id From User Where id =:'00590000000O5cX'];
         System.runAs(u ) {
             MsrRequestInfoUpdate.inputClass input = new MsrRequestInfoUpdate.inputClass();
             input.downloadAll = true;
             input.lastRequestDate = date.today();
             MsrRequestInfoUpdate.getIncrementalMedical(input);
         }
     }

}